package com.koron.order.workbench.appMenu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.workbench.appMenu.bean.dto.AppMenuDto;
import com.koron.order.workbench.appMenu.bean.po.AppMenuPo;
import com.koron.order.workbench.appMenu.mapper.AppMenuMapper;
import com.koron.order.workbench.appMenu.service.AppMenuService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 个人应用菜单表(AppMenu)表服务实现类
 *
 * @author zhouj
 * @since 2022-03-18 08:35:25
 */
@Service("appMenuService")
public class AppMenuServiceImpl implements AppMenuService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, AppMenuPo appMenu) {

        AppMenuMapper mapper = factory.getMapper(AppMenuMapper.class);

        return mapper.insertOrUpdate(appMenu);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<AppMenuPo> appMenuList) {

        AppMenuMapper mapper = factory.getMapper(AppMenuMapper.class);

        return mapper.insertOrUpdateBatch(appMenuList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, AppMenuPo appMenu) {

        AppMenuMapper mapper = factory.getMapper(AppMenuMapper.class);

        return mapper.insert(appMenu);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, AppMenuDto appMenuDto) {

        AppMenuMapper mapper = factory.getMapper(AppMenuMapper.class);

        String appId = appMenuDto.getAppId();
        String account = appMenuDto.getAccount();

        Assert.isTrue(StringUtils.isNotEmpty(appId));
        Assert.isTrue(StringUtils.isNotEmpty(account));

        mapper.deleteByAppId(appId, account);

        List<AppMenuPo> appMenuPoList = appMenuDto.getAppMenuPoList();

        if (CollectionUtils.isEmpty(appMenuPoList)) {
            return 0;
        }
        appMenuPoList.forEach(p -> {
            p.setAppId(appId);
            p.setAccount(account);
        });

        return mapper.insertBatch(appMenuPoList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, AppMenuPo appMenu) {
        AppMenuMapper mapper = factory.getMapper(AppMenuMapper.class);
        return mapper.update(appMenu);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        AppMenuMapper mapper = factory.getMapper(AppMenuMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByAppId")
    public int deleteByAppId(SessionFactory factory, String appId, String account) {

        AppMenuMapper mapper = factory.getMapper(AppMenuMapper.class);
        return mapper.deleteByAppId(appId, account);
    }

    @Override
    @TaskAnnotation("queryById")
    public AppMenuPo queryById(SessionFactory factory, String id) {

        AppMenuMapper mapper = factory.getMapper(AppMenuMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<AppMenuPo> queryList(SessionFactory factory, AppMenuPo appMenu) {

        AppMenuMapper mapper = factory.getMapper(AppMenuMapper.class);

        return mapper.queryList(appMenu);
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, AppMenuPo appMenu, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        AppMenuMapper mapper = factory.getMapper(AppMenuMapper.class);

        List<AppMenuPo> list = mapper.queryList(appMenu);

        return new PageInfo(list);
    }

}
